package cn.edu.njust.epms.dao;

import cn.edu.njust.epms.entity.ATRecord;
import cn.edu.njust.epms.entity.ATResv;
import cn.edu.njust.epms.entity.UserDetails;
import org.apache.ibatis.annotations.*;
import cn.edu.njust.epms.common.R;

import java.util.Date;
import java.util.List;

@Mapper
public interface ATDao {
    /**
     * 新增核酸记录
     * @param record
     * @return
     */
    @Insert("insert into at_record (pid,aTime,aResult) values(#{pid},#{aTime},#{aResult})")
    public boolean Add(ATRecord record);

    /**
     * 更新核酸结果
     * @param r
     * @param aId
     * @return
     */
    @Update("update at_record set aResult = #{r} where aId = #{aId}")
    public boolean updateATResult(Integer r,Integer aId);


    /**
     * 通用查询
     * @param r
     * @return
     */
    @Select("<script>"+
            "SELECT * FROM at_record inner join udetails on udetails.pid=at_record.pid"+
            "<where>"+
            "   <if test='r.name != null and r.name.length > 0'>"+
            "       AND name like #{r.name}"+
            "   </if>"+
            "   <if test='r.aResult != null'>"+
            "       AND aResult = #{r.aResult}"+
            "   </if>"+
            "   <if test='r.uId != null'>"+
            "       AND udetails.uId = #{r.uId}"+
            "   </if>"+
            "   <if test='r.pid != null and r.pid.length > 0'>"+
            "       AND udetails.pid like #{r.pid}"+
            "   </if>"+
            "   <if test='r.date1 != null and r.date2 != null'>"+
            "       AND aTime between #{r.date1} and #{r.date2}"+
            "   </if>"+
            "</where>"+
            "</script>")
    @Results({
            @Result(column = "aId",property = "aId"),
            @Result(column = "aTime",property = "aTime"),
            @Result(column = "aResult",property = "aResult"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "pid",property = "ud.pid"),
            @Result(column = "name",property = "ud.name")
    })
    public List<ATRecord> selectByCondition(@Param("r") R r);

    @Insert("insert into at_resv (pid, rTime, sts,aTime) VALUES (#{pid},#{rTime},#{sts},#{aTime})")
    public Boolean addResv(ATResv ar);

    @Update("update at_resv set sts=#{sts} where rId=#{rId}")
    public Boolean updateResv(ATResv ar);

    @Select("<script>"+
            "SELECT * FROM at_resv inner join udetails on udetails.pid=at_resv.pid"+
            "<where>"+
            "   <if test='r.name != null and r.name.length > 0'>"+
            "       AND name like #{r.name}"+
            "   </if>"+
            "   <if test='r.sts != null'>"+
            "       AND sts = #{r.sts}"+
            "   </if>"+
            "   <if test='r.uId != null'>"+
            "       AND udetails.uId = #{r.uId}"+
            "   </if>"+
            "   <if test='r.pid != null and r.pid.length > 0'>"+
            "       AND udetails.pid like #{r.pid}"+
            "   </if>"+
            "   <if test='r.date1 != null and r.date2 != null'>"+
            "       AND rTime between #{r.date1} and #{r.date2}"+
            "   </if>"+
            "   <if test='r.date3 != null and r.date4 != null'>"+
            "       AND aTime between #{r.date3} and #{r.date4}"+
            "   </if>"+
            "</where>"+
            "</script>")
    @Results({
            @Result(column = "rId",property = "rId"),
            @Result(column = "rTime",property = "rTime"),
            @Result(column = "sts",property = "sts"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "aTime",property = "aTime"),
            @Result(column = "pid",property = "ud.pid"),
            @Result(column = "name",property = "ud.name"),
            @Result(column = "addr",property = "ud.addr"),
            @Result(column = "phone",property = "ud.phone"),
            @Result(column = "gender",property = "ud.gender")
    })
    public List<ATResv> selectResvByCondition(@Param("r") R r);

    @Delete("delete from at_record where aId=#{aId}")
    public Boolean deleteATRecord(Integer aId);

    @Delete("delete from at_resv where rId=#{rId}")
    public Boolean deleteATResv(ATResv r);

    @Select("SELECT * FROM udetails WHERE pid not in (" +
            "SELECT pid FROM at_record WHERE at_record.aTime BETWEEN #{date1} AND #{date2})")
    public List<UserDetails> selectUdWithoutAT(Date date1, Date date2);

    @Select("select * from at_record where aId=#{aId}")
    public ATRecord selectByAid(Integer aId);
}
